﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using PropertyInfoDataProvider.Properties;

namespace PropertyInfoDataProvider
{
    public class PropertyInfoProvider
    {
        private Empdata _dataSource;

        public PropertyInfoProvider()
        {
            _dataSource = new Empdata(Settings.Default.empdataConnectionString);
        }

        private string GetAreaFromMessageClass(MessageClass messageClass)
        {
            switch(messageClass)
            {
                case MessageClass.Common:
                    return "Common";
                case MessageClass.Email:
                    return "Message";
                case MessageClass.Calendar:
                    return "Calendar";
                case MessageClass.Contact:
                    return "Contact";
                case MessageClass.Task:
                    return "Task";
                case MessageClass.Journal:
                    return "Journal";
                case MessageClass.StickyNote:
                    return "Sticky Note";
                case MessageClass.Document:
                    return "Document";
                case MessageClass.Post:
                    return "Post";
                case MessageClass.RssFeed:
                    return "RSS Feed";
                case MessageClass.RightsManagedEmail:
                    return "Rights Managed Email";
                case MessageClass.SMimeEmail:
                    return "S/MIME Email";
                case MessageClass.SmsMms:
                    return "Short message service / Multimedia Message Service Message";
                case MessageClass.UnifiedMessaging:
                    return "Unified messaging";
                case MessageClass.Sharing:
                    return "Sharing";
                default:
                    return "Common";
            }
        }

        public IPropertyInformation GetPropertyInformation(int propertyId)
        {
            return
                new PropertyInformation(
                    _dataSource.TblProperty.Where(x => x.Property_ID == propertyId).Select(y => y).FirstOrDefault());
        }

        /// <summary>
        /// Tries to find property information constrained by the MessageClass.  If a property in the specific
        /// MessageClass does not exist, it will look in the "Common" MessageClass first, otherwise it will return
        /// the first result found or null
        /// </summary>
        /// <param name="propertyId">Property ID to search for</param>
        /// <param name="messageClass"></param>
        /// <returns></returns>
        public IPropertyInformation GetPropertyInformation(int propertyId, MessageClass messageClass)
        {
            var properties = _dataSource.TblProperty.Where(x => x.Property_ID == propertyId).Select(y => y).ToArray();

            if(properties.Length == 0)
                return default(PropertyInformation);

            if (properties.Length == 1)
                return new PropertyInformation(properties.First());

            var area = GetAreaFromMessageClass(messageClass);

            if (!properties.Any(x => x.Area.Contains(area)))
            {
                if (properties.Any(x => x.Area.Contains("Common")))
                    return new PropertyInformation(properties.Where(x => x.Area.Contains("Common")).First());

                return new PropertyInformation(properties.First());
            }

            return new PropertyInformation(properties.Where(x => x.Area.Contains(area)).First());
        }
    }
}
